package models

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"time"
	//"errors"
	"fmt"
	// "reflect"
	// "strings"
)

type User struct {
	Id           int       `orm:"column(Id);auto"`
	UserName     string    `orm:"column(UserName);null"`
	TrueName     string    `orm:"column(TrueName);null"`
	Gender       int       `orm:"column(Gender);size(250);null"`
	Address      string    `orm:"column(Address);size(250);null"`
	Mobile       string    `orm:"column(Mobile);size(250);null"`
	QQ           string    `orm:"column(Qq);size(250);null"`
	Email        string    `orm:"column(Email);size(50);null"`
	Tel          string    `orm:"column(Tel);size(250);null"`
	Fax          string    `orm:"column(Fax);size(250);null"`
	Site         string    `orm:"column(Site);size(250);null"`
	Logo         string    `orm:"column(Logo);size(250);null"`
	Industry     int       `orm:"column(IndustryId);null"`
	Provinces    int       `orm:"column(ProvincesId);null"`
	City         int       `orm:"column(CityId);null"`
	DesCription  string    `orm:"column(DesCripe);size(4000);null"`
	Remarks      string    `orm:"column(Remarks);size(4000);null"`
	DepartmentId int       `orm:"column(DepartmentId);null"`
	CreateId     int       `orm:"column(CreateId);null"`
	CreateName   string    `orm:"column(CreateName);size(250);null"`
	CreateDate   time.Time `orm:"column(CreateDate);auto_now_add;type(datetime);null"`
	UpdateDate   time.Time `orm:"column(UpdateDate);auto_now;type(datetime);null"`
	IsDelete     int       `orm:"column(IsDelete);null"`
	DeleteTime   time.Time `orm:"column(DeleteTime);type(datetime);null"`
	Types        int       `orm:"column(Types);null"`
}

func init() {
	orm.RegisterModel(new(User))
}

func (t *User) TableName() string {
	return "057dg_user"
}

func (t *User) AddUser(m *User) (id int64, err error) {
	orm := orm.NewOrm()
	id, err = orm.Insert(m)
	return
}

func GetUserById(id int) (v *User, err error) {
	o := orm.NewOrm()
	v = &User{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetUserByEmail(em string) (v *User, err error) {
	o := orm.NewOrm()
	v = &User{Email: em}
	err = o.Read(v)
	return
	// if err != nil {
	// 	return v, nil
	// }
	// return nil, err
}

// func findAll(query []string, fields []string, sortby []string, order []string, offset int64, limit int64) (ml []interface{}, err error) {}
func (t *User) FindAllUser() (ml []interface{}, err error) {
	orm := orm.NewOrm()
	qs := orm.QueryTable(t.TableName())
	var cus []User
	if _, err := qs.All(&cus); err == nil {
		for _, v := range cus {
			ml = append(ml, v)
		}
		return ml, nil
	}
	return nil, err
}

func UpdateCustomerById(m *User) (err error) {
	o := orm.NewOrm()
	v := User{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

func DeleteCustomer(id int) (err error) {
	o := orm.NewOrm()
	v := User{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&User{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}
